<!--
 * @Author: 大国男儿 7824741+lc2677@user.noreply.gitee.com
 * @Date: 2022-10-22 22:07:55
 * @LastEditors: 大国男儿 7824741+lc2677@user.noreply.gitee.com
 * @LastEditTime: 2022-10-28 19:35:42
 * @FilePath: \vue-echarts\src\components\Rank.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      chartInstance: null,
      allDate: null, // 服务器返回的数据
      startValue: 0, // 区域缩放的起点值和终点值
      endValue: 8,
      timerId: null // 定时器的标识
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    // 适配屏幕大小
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed () {
    // 取消定时器
    clearInterval(this.timerId)
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    // 初始化图表
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, 'chalk')
      const initOption = {
        title: {
          text: '地区销售排行',
          left: 20,
          top: 20
        },
        grid: {
          top: '40%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true // 是否包含坐标轴上的文字
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar',
            markPoint: {
              data: [
                {
                  type: 'max', name: '最大值'
                },
                {
                  type: 'min', name: '最小值'
                }
              ]
            },
            label: {
              show: true,
              // rotate: 60,
              position: 'inside'
            }
          }
        ]
      }
      this.chartInstance.setOption(initOption)
      // 添加鼠标移入移出监听
      this.chartInstance.on('mouseover', () => {
        // 关闭定时器
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        // 开启定时器
        this.startInterval()
      })
    },
    async getData () {
      // http://127.0.0.1:8888/api/rank
      const { data: ret } = await this.$http.get('rank')
      this.allDate = ret
      // 进行排序从大到小
      this.allDate.sort((a, b) => {
        return b.value - a.value
      })
      console.log(this.allDate)
      // 更新图表
      this.updateChart()
      // 获取到数据之后，启动定时器
      this.startInterval()
    },
    updateChart () {
      const colorArr = [
        ['#0ba82c', '#4ff778'],
        ['#2e72bf', '#23e5e5'],
        ['#5052ee', '#ab6ee5']
      ]
      // 省份数组
      const provinceArr = this.allDate.map(item => {
        return item.name
      })
      // 省份对应的金额
      const valueArr = this.allDate.map(item => {
        return item.value
      })
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        // 区域缩放
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: {
          data: valueArr,
          itemStyle: {
            // 显示不同的颜色
            color: arg => {
              console.log(arg)
              let targetColor = null
              if (arg.data > 300) {
                targetColor = colorArr[0]
              } else if (arg.data > 200) {
                targetColor = colorArr[1]
              } else {
                targetColor = colorArr[2]
              }
              return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: targetColor[0]
                },
                {
                  offset: 1,
                  color: targetColor[1]
                }
              ])
            }
          }
        }
      }
      this.chartInstance.setOption(dataOption)
    },
    // 让数据每隔一端时间向左移动一条数据，通过控制dataZoom的startValue和endValue
    // 定时器
    startInterval () {
      // 保险判断
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      // 设置定时器
      this.timerId = setInterval(() => {
        this.startValue++
        this.endValue++
        // 边界判断
        if (this.endValue > this.allDate.length) {
          // 设置为初始值
          this.startValue = 0
          this.endValue = 8
        }
        this.updateChart()
      }, 2000)
    },
    screenAdapter () {
      // 获取容器宽度
      const titleFontSize = this.$refs.rank_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize, // 柱宽
            itemStyle: {
              borderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    }

  }

}
</script>

<style>

</style>
